package com.preskool.controller;

import com.github.pagehelper.PageInfo;
import com.preskool.dto.ActionResult;
import com.preskool.po.Hostel;
import com.preskool.service.HostelService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hostel")
public class HostelController {

    @Autowired(required = false)
    private HostelService hostelService;

    @RequestMapping(value = "/findAllHostel", method = RequestMethod.GET)
    @ResponseBody
    public ActionResult findAllHostel(Integer currentPage, Integer pageSize) {

        ActionResult actionResult = new ActionResult();
        List<Hostel> allHostel = hostelService.findAllHostel(currentPage,pageSize);
        PageInfo<Hostel> hostelPageInfo = new PageInfo<>(allHostel);
        //总条数
        actionResult.setCount(hostelPageInfo.getTotal());
        actionResult.setPageNum(hostelPageInfo.getPageNum());
        actionResult.setTotalPage(hostelPageInfo.getPageSize());
        actionResult.setData(allHostel);
        actionResult.setMsg("查询成功");
        actionResult.setStatusCode(200);
        return actionResult;

    }

    @RequestMapping(value = "/delHostel", method = RequestMethod.GET)
    @ResponseBody
    public ActionResult delHostel(Integer roomNo) {
        Integer del = hostelService.delHostel(roomNo);
        return new ActionResult(200, "删除成功", null, null, null, null);


    }


    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public ActionResult update(Hostel hostel, HttpServletRequest request) {
//        //根据传入对象获得id
//        Integer roomNo = hostel.getRoomNo();
        Boolean hostel1 = hostelService.update(hostel);
//处理中文乱码
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
//        if (hostel1 != null) {
//            Integer update = hostelService.update(hostel1);
//            hostel1.setBlock(hostel.getBlock());
//            hostel1.setRoomType(hostel.getRoomType());
//            hostel1.setAvailability(hostel.getAvailability());
//            hostel1.setNoOfBeds(hostel.getNoOfBeds());
//            hostel.setCostPerBed(hostel.getCostPerBed());
//            return update;
//        }

        return new ActionResult(200, "修改成功", hostel1, null, null, null);
    }

    @RequestMapping(value = "/findOneByroomNo", method = RequestMethod.GET)
    @ResponseBody
    public ActionResult findOneByroomNo(Integer roomNo) {
        // Integer roomNo = Integer.valueOf(map.get("roomNo").toString());
        Hostel oneByroomNo = hostelService.findOneByroomNo(roomNo);
        return new ActionResult(200, "查一个成功", oneByroomNo, null, null, null);

    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Integer add(Hostel hostel) {
        Integer add = hostelService.add(hostel);
        return add;
    }

    @RequestMapping(value = "/findByType", method = RequestMethod.GET)
    public ActionResult findByType(String roomType) {
        List<Hostel> byType = hostelService.findByType(roomType);
        return new ActionResult(200, "模糊查询结果如下", byType, null, null, null);

    }


}
